package com.financing.task.invest;
import com.financing.model.invest.Bid;
import com.financing.service.invest.BidService;
import com.financing.service.invest.BidVirtualLogService;
import com.financing.service.invest.RepayService;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;

/**
 * 还款计划任务
 * @author 无处不在的二叔
 */
public class AutoRepayTask implements Runnable{

	private static BidService bid = new BidService();	
	private static BidVirtualLogService bidVirtualLog = new BidVirtualLogService();
    
	@Override 
	@Before(Tx.class)
	public void run() {
		
		// 自动虚增未满标并且过投资期限的标的
		for(Bid b : bid.expireInvest()) {
			try {
				b.setIsTrading(1).update();
				int unit = b.getUnit() - b.getApplyUnit() - b.getVirtualUnit();
				bidVirtualLog.plus(b.getId(), unit, 0);
			} catch(Exception e) {
				e.printStackTrace();
			} finally {
				b.setIsTrading(0).update();
			}
		}
		
		//更新全虚增标的的状态为已还款
		bid.finishVirtual();
		
		// 开始常规还款任务
		RepayService.run(0);
		
	}
}
